package org.misalen.web.admin.controllers;

import java.util.Date;

import org.misalen.utils.TextUtil;
import org.misalen.web.admin.util.RedPacket;
import org.misalen.web.admin.util.ServerResult;
import org.misalen.web.database.model.mysql.pro.ProOwnerUser;
import org.misalen.web.database.model.mysql.pro.ProVideoInfo;
import org.misalen.web.database.repository.mysql.ProVideoInfoRepository;
import org.misalen.web.database.util.PaginationFrom;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

@Controller
@RequestMapping(value = "/pro-video-info")
public class ProVideoInfoController extends BaseController {

	@Autowired
	protected ProVideoInfoRepository proVideoInfoRepository;

	/**
	 * 列表页
	 * 
	 * @return 列表目标页
	 */
	@RequestMapping(value = "/", method = { RequestMethod.GET })
	public ModelAndView list() {
		ModelAndView modelAndView = new ModelAndView("/proVideoInfo/list");
		modelAndView.addObject("belongCity", getShiroUser().getBelongCity());
		return modelAndView;
	}

	/**
	 * 列表
	 * 
	 * @param 搜索条件
	 * @param 分页/排序条件
	 * @return 查询结果
	 */
	@RequestMapping(value = "/", method = { RequestMethod.POST })
	public @ResponseBody ServerResult listPost(ProVideoInfo proVideoInfo, PaginationFrom<ProVideoInfo> pagination) {
		String belongCity = getShiroUser().getBelongCity();
		if(!belongCity.equals("全国")) {
			proVideoInfo.setAreaScope(belongCity);
		}
		
		PaginationFrom<ProVideoInfo> from = proVideoInfoRepository.findPageByBean(proVideoInfo, pagination);
		return renderSuccess(from);
	}

	/**
	 * 添加页面
	 * 
	 * @return 添加目标页
	 */
	@RequestMapping(value = "/add", method = { RequestMethod.GET })
	public ModelAndView add() {
		ModelAndView modelAndView = new ModelAndView("/proVideoInfo/add");
		return modelAndView;
	}

	/**
	 * 添加动作
	 * 
	 * @param 信息
	 * @return 添加后的信息
	 */
	@RequestMapping(value = "/add", method = { RequestMethod.POST })
	public @ResponseBody ServerResult addPost(ProVideoInfo proVideoInfo, String userMobile) {
		ProOwnerUser proUser = proOwnerUserRepository.findByMobile(userMobile);
		if (proUser == null) {
			return renderError("用户不存在");
		}
		if (proVideoInfo.getStartDate().getTime() >= proVideoInfo.getStopDate().getTime()) {
			return renderError("时间范围不正确");
		}
		int[] is = RedPacket.generate(proVideoInfo.getRedEnvelopTotal(), proVideoInfo.getDriverNum(),
				proVideoInfo.getRedEnvelopMin(), proVideoInfo.getRedEnvelopMax());
		if (is == null) {
			return renderError("红包无法分配");
		} else {
			proVideoInfo.setUserId(proUser.getPrimaryKey());
			proVideoInfo.setAddTime(new Date());
			long time=System.currentTimeMillis();
			if(proVideoInfo.getStartDate().getTime()<time&&proVideoInfo.getStopDate().getTime()>time){
				proVideoInfo.setTimeLimit(TextUtil.booleanToString(true));
			}else{
				proVideoInfo.setTimeLimit(TextUtil.booleanToString(false));
			}
			proVideoInfo.setGrabThe(TextUtil.booleanToString(false));
			proVideoInfo.setDriverPlayNum(0);
			proVideoInfoRepository.save(proVideoInfo);

			long nd = 1000 * 24 * 60 * 60;
			long diff = proVideoInfo.getStopDate().getTime() - proVideoInfo.getStartDate().getTime();
			proVideoRewardRepository.saveList(is, proVideoInfo.getPrimaryKey(), diff / nd * 10,proVideoInfo.getTimeLimit());
			return renderSuccess(proVideoInfo);
		}
	}

	/**
	 * 更新页面
	 * 
	 * @param 主键
	 * @return 更新目标页
	 */
	@RequestMapping(value = "/{primaryKey}/update", method = { RequestMethod.GET })
	public ModelAndView update(@PathVariable String primaryKey) {
		ProVideoInfo proVideoInfo = proVideoInfoRepository.get(ProVideoInfo.class, primaryKey);
		proVideoInfo.setUserName(proOwnerUserRepository.get(ProOwnerUser.class, proVideoInfo.getUserId()).getUserName());
		ModelAndView modelAndView = new ModelAndView("/proVideoInfo/update");
		modelAndView.addObject("info", proVideoInfo);
		return modelAndView;
	}

	/**
	 * 更新动作
	 * 
	 * @param 信息
	 * @return 更新后的信息
	 */
	@RequestMapping(value = "/update", method = { RequestMethod.POST })
	public @ResponseBody ServerResult updatePost(ProVideoInfo proVideoInfo) {
		ProVideoInfo proVideoInfo2 = proVideoInfoRepository.get(ProVideoInfo.class, proVideoInfo.getPrimaryKey());
		proVideoInfo2.setVideoName(proVideoInfo.getVideoName());
		proVideoInfo2.setVideoUrl(proVideoInfo.getVideoUrl());
		proVideoInfo2.setVideoThumUrl(proVideoInfo.getVideoThumUrl());
		proVideoInfoRepository.update(proVideoInfo2);
		return renderSuccess(proVideoInfo2);
	}

	/**
	 * 删除
	 * 
	 * @param 主键
	 * @return 删除结果
	 */
	@RequestMapping(value = "/{primaryKey}/del", method = { RequestMethod.GET })
	public @ResponseBody Boolean del(@PathVariable String primaryKey) {
		proVideoInfoRepository.delete(ProVideoInfo.class, primaryKey);
		proVideoRewardRepository.removeReward(primaryKey);
		proVideoRewardRepository.deleteRewardByVideo(primaryKey);
		return true;
	}
}
